home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 25 / CU Amiga Magazine's Super CD-ROM 25 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-08].iso / CUCD / Programming / QuakeTools / src / libqtools / script.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-11  |  23.8 KB  |  701 lines

  1. #define    LIBQTOOLS_CORE
  2. #include "../include/libqtools.h"
  3. #include "../include/libqbuild.h"
  4.  
  5. /*
  6.  * Script-System:
  7.  * 
  8.  * commands:
  9.  * "process <file>.<ext>"                               -> take this file upto
  10.  * "finish <file>.<ext>"                                -> here
  11.  * "list <file>.<ext>"                    -> list the contents
  12.  * "view <file>.<ext>"                    -> view the pictures or texts in a window, or whatever
  13.  * 
  14.  * sub-commands <PAK>:
  15.  * "OP_UPDATE <file>.<ext> as <name>"                   -> OP_ADD if not exists, OP_REPLACE if newer
  16.  * "OP_REPLACE <file>.<ext> as <name>"                  -> OP_REPLACE if exists, do NOT OP_ADD
  17.  * "OP_ADD <file>.<ext> as <name>"                      -> OP_ADD if not exists, do NOT OP_REPLACE
  18.  * "OP_DELETE <name>"                                   -> OP_DELETE if exists
  19.  * "OP_EXTRACT <name> as <file>.<ext>"                  -> OP_EXTRACT if exists
  20.  * 
  21.  * sub-commands <WAD>:
  22.  * -multiple same names are allowed, but with different types
  23.  * "OP_UPDATE <file>.<ext> as <name> as <type>"         -> OP_ADD if not exists, OP_REPLACE if newer
  24.  * "OP_REPLACE <file>.<ext> as <name> as <type>"        -> OP_REPLACE if exists, do NOT OP_ADD
  25.  * "OP_ADD <file>.<ext> as <name> as <type>"            -> OP_ADD if not exists, do NOT OP_REPLACE
  26.  * "OP_DELETE <name> as <type>"                         -> OP_DELETE if exists
  27.  * "OP_EXTRACT <name> as <file>.<ext> as <type>"        -> OP_EXTRACT if exists
  28.  * 
  29.  * sub-commands <BSP>:
  30.  * sub-commands <MDL>:
  31.  * 
  32.  */
  33.  
  34. /*
  35.  * basic main-skeleton
  36.  *
  37.  * extension-type system:
  38.  *  .wad -> wad-archive
  39.  *  .bsp -> bsp
  40.  *  .pak -> pak-archive
  41.  *  .mdl -> models
  42.  *  .spr -> sprites
  43.  *
  44.  *  .ppm -> ppm-picture
  45.  *  .mip -> miptexture
  46.  *  .lmp -> console-picture
  47.  *  .stb -> statusbar-picture
  48.  *  .pal -> palettes
  49.  *  .wav -> wave-sounds
  50.  *  .dat -> pseudo-code
  51.  *  .rc  -> resource
  52.  *  .cfg -> config
  53.  *  .tri -> triangle
  54.  *  .map -> map
  55.  *  .skn -> skin-picture (lmp)
  56.  *  .frm -> frame-picture (lmp)
  57.  *
  58.  * hierachy-system:
  59.  *   game                                               -> pak?.pak
  60.  *   game/pak?.dir                                      -> ?.dat, ...
  61.  *   game/pak?.dir/gfx                                  -> ?.lmp
  62.  *   game/pak?.dir/maps                                 -> ?.bsp
  63.  *   game/pak?.dir/maps/?.dir                           -> ?.map, ?.wad
  64.  *   game/pak?.dir/maps/?.dir/?.dir                     -> ?.mip/ppm, ?.lmp, ?.pal, ?.stb
  65.  *   game/pak?.dir/progs                                -> ?.mdl, ?.spr
  66.  *   game/pak?.dir/progs/?.dir                          -> ?-?.tri, ?-?.skn
  67.  *   game/pak?.dir/progs/?.dir                          -> ?-?.frm
  68.  *   game/pak?.dir/sounds                               -> ?.wav
  69.  * example:
  70.  *   game/pak0.pak
  71.  *   game/pak0.dir/default.cfg                          -> pak0.pak  (makpak)
  72.  *   game/pak0.dir/gfx/colormap.lmp                     -> pak0.pak  (makpak)
  73.  *   game/pak0.dir/gfx/conback.lmp                      -> pak0.pak  (repak)
  74.  *   game/pak0.dir/gfx/palette.lmp                      -> pak0.pak  (repak)
  75.  *   game/pak0.dir/maps/start.bsp                       -> pak0.pak  (repak)
  76.  *   game/pak0.dir/maps/start.dir/start.map             -> start.bsp (qbsp)
  77.  *   game/pak0.dir/maps/start.dir/start.wad             -> start.bsp (qbsp)
  78.  *   game/pak0.dir/maps/start.dir/start.dir/text1.mip   -> start.wad (rewad)
  79.  *   game/pak0.dir/maps/start.dir/start.dir/text2.mip   -> start.wad (rewad)
  80.  *   game/pak0.dir/progs/ogre.mdl                       -> pak0.pak  (repak)
  81.  *   game/pak0.dir/progs/ogre.dir/ogre-0.tri            -> ogre.mdl  (mdlgen)
  82.  *   game/pak0.dir/progs/ogre.dir/ogre-1.tri            -> ogre.mdl  (mdlgen)
  83.  *   game/pak0.dir/progs/ogre.dir/ogre-0.skn            -> ogre.mdl  (mdlgen)
  84.  *   game/pak0.dir/progs/fire.spr                       -> pak0.pak  (repak)
  85.  *   game/pak0.dir/progs/fire.dir/fire-0.frm            -> fire.spr  (sprgen)
  86.  *   game/pak0.dir/progs/fire.dir/fire-1.frm            -> fire.spr  (sprgen)
  87.  */
  88.  
  89. bool processType(char *procName, filetype procType,
  90.          char *destDir,
  91.          char *outName, filetype outType,
  92.          char *arcName, filetype arcType,
  93.          operation procOper, 
  94.          bool script, bool recurse) {
  95.   FILE *destFile = 0, *scrpFile = 0;
  96.   FILE *procFile = 0;
  97.  
  98. #ifdef    PRINTCALLS
  99.   mprintf("processType(%s, %d, %s, %s, %d, %s, %d, %d, %d, %d)\n", procName, procType, destDir, outName, outType, arcName, arcType, procOper, script, recurse);
  100. #endif
  101.  
  102.   if ((procFile = fopen(procName, READ_BINARY))) {
  103.     /*
  104.      * get extension 
  105.      */
  106.     if ((procType == TYPE_PPM) && (!outType)) {
  107.       eprintf("you must specify an output if i should convert from an ppm\n");
  108.     }
  109.     else {
  110.       struct palpic *inPic = 0;
  111.       struct rawdata *inData = 0;
  112.       char idxName[NAMELEN_PATH + 1];
  113.  
  114.       strncpy(idxName, destDir, NAMELEN_PATH - 1);
  115.       strncat(idxName, procName, NAMELEN_PATH - 1);
  116.       ReplaceExt(idxName, "idx");
  117.  
  118.       /*
  119.        * script-based execution 
  120.        */
  121.       if (procType == TYPE_INDEX) {
  122.     char command[NAMELEN_PATH], parameter[NAMELEN_PATH];
  123.     char *nameStack[NAMELEN_PATH]; short int nameCount = 0;
  124.     char *thisName;
  125.     nameStack[nameCount] = 0;
  126.  
  127.     /*
  128.      * process are now stackable -> proces ... process ... finish ... finish
  129.      */
  130.     while (fscanf(procFile, "%256s %256[^\n]\n", command, parameter) != EOF) {
  131.       if (!strcmp(command, "process")) {
  132.         if(!(thisName = nameStack[++nameCount] = smalloc(parameter)))
  133.           eprintf("cannot allocate process-stack");
  134.       }
  135.       else if ((!strcmp(command, "dither")) && (thisName)) {
  136.         char Bool;
  137.         int Value;
  138.  
  139.         sscanf(parameter, "%c with %d", &Bool, &Value);
  140.         if (Bool == 1) {
  141.           /*
  142.            * activate dithering with specified value 
  143.            */
  144.           dither = TRUE;
  145.           dithervalue = Value;
  146.         }
  147.         else if (Bool == 0) {
  148.           /*
  149.            * deactivate dithering and reset dithervalue 
  150.            */
  151.           dither = FALSE;
  152.           dithervalue = 16;
  153.         }
  154.       }
  155.       else if ((!strcmp(command, "compress")) && (thisName)) {
  156.         char Bool;
  157.         int Value;
  158.  
  159.         sscanf(parameter, "%c with %d", &Bool, &Value);
  160.         if (Bool == 1)
  161.           Compression = Value;
  162.         else if (Bool == 0)
  163.           Compression = CMP_NONE;
  164.       }
  165.       else if ((!strcmp(command, "update")) && (thisName)) {
  166.         char fileName[NAMELEN_PATH], entryName[NAMELEN_MAXQUAKE], entryType;
  167.  
  168.         sscanf(parameter, "%256s as %56s as %c", fileName, entryName, &entryType);
  169.         processName(fileName, destDir, thisName, outType, entryName, entryType, OP_UPDATE, script, recurse);
  170.       }
  171.       else if ((!strcmp(command, "replace")) && (thisName)) {
  172.         char fileName[NAMELEN_PATH], entryName[NAMELEN_MAXQUAKE], entryType;
  173.  
  174.         sscanf(parameter, "%256s as %56s as %c", fileName, entryName, &entryType);
  175.         processName(fileName, destDir, thisName, outType, entryName, entryType, OP_REPLACE, script, recurse);
  176.       }
  177.       else if ((!strcmp(command, "add")) && (thisName)) {
  178.         char fileName[NAMELEN_PATH], entryName[NAMELEN_MAXQUAKE], entryType;
  179.  
  180.         sscanf(parameter, "%256s as %56s as %c", fileName, entryName, &entryType);
  181.         processName(fileName, destDir, thisName, outType, entryName, entryType, OP_ADD, script, recurse);
  182.       }
  183.       else if ((!strcmp(command, "delete")) && (thisName)) {
  184.         char entryName[NAMELEN_MAXQUAKE], entryType;
  185.  
  186.         sscanf(parameter, "%56s as %c", entryName, &entryType);
  187.         processName(thisName, 0, 0, outType, entryName, entryType, OP_DELETE, script, recurse);
  188.       }
  189.       else if ((!strcmp(command, "extract")) && (thisName)) {
  190.         char fileName[NAMELEN_PATH], entryName[NAMELEN_MAXQUAKE], entryType;
  191.  
  192.         sscanf(parameter, "%256s as %56s as %c", entryName, fileName, &entryType);
  193.         processName(thisName, destDir, fileName, outType, entryName, entryType, OP_EXTRACT, script, recurse);
  194.       }
  195.       else if ((!strcmp(command, "finish")) && (thisName)) {
  196.         tfree(nameStack[nameCount]);
  197.         thisName = nameStack[--nameCount];
  198.       }
  199.       else
  200.         eprintf("unknown command, cannot execute line \"%s\" ... \n", command);
  201.     }
  202.     /*
  203.      * tfree resources in case of unfinished processes 
  204.      */
  205.     while(nameCount)
  206.       tfree(nameStack[nameCount--]);
  207.       }
  208.       /*
  209.        * directory-based execution 
  210.        */
  211.       else if (procType == TYPE_DIRECTORY) {
  212.     DIR *procDir = 0;
  213.     struct dirent *dirEnt = 0;
  214.  
  215.     if((procDir = opendir(procName))) {
  216.       while((dirEnt = readdir(procDir))) {
  217.         if(strcmp(dirEnt->d_name, ".") && strcmp(dirEnt->d_name, "..")) {
  218.           char dirName[NAMELEN_PATH];
  219.         
  220.           strncpy(dirName, procName, NAMELEN_PATH - 1);
  221.           strncat(dirName, "/", NAMELEN_PATH - 1);
  222.           strncat(dirName, dirEnt->d_name, NAMELEN_PATH - 1);
  223.           /*
  224.            * if outName == 0, process the dir entirely
  225.            * else add the complete to outName
  226.            */
  227.           processName(dirName, destDir, outName, outType, 0, arcType, procOper, script, recurse);
  228.         }
  229.       }
  230.       closedir(procDir);
  231.     }
  232.     else
  233.       eprintf("cannot access directory %s\n", procName);
  234.       }
  235.       /*
  236.        * parts to archive or ppm 
  237.        */
  238.       else if ((procType == TYPE_PALETTE) ||
  239.            (procType == TYPE_CONFIG) ||
  240.            (procType == TYPE_DEMO) ||
  241.            (procType == TYPE_FRAME) ||
  242.            (procType == TYPE_ILBM) ||
  243.            (procType == TYPE_JPEG) ||
  244.            (procType == TYPE_LIT) ||
  245.            (procType == TYPE_LUMP) ||
  246.            (procType == TYPE_MAP) ||
  247.            (procType == TYPE_MIPMAP) ||
  248.            (procType == TYPE_PNG) ||
  249.            (procType == TYPE_PPM) ||
  250.            (procType == TYPE_RESOURCE) ||
  251.            (procType == TYPE_SKIN) ||
  252.            (procType == TYPE_STATUSBAR) ||
  253.            (procType == TYPE_TRIANGLE) ||
  254.            (procType == TYPE_VIS) ||
  255.            (procType == TYPE_WAL) ||
  256.            (procType == TYPE_WAVE)) {
  257.     if (procType == TYPE_MIPMAP) {
  258.       mprintf("read mipmap %s\t-> ", procName);
  259.       inPic = GetMipMap(procFile, 0);
  260.     }
  261.     if (procType == TYPE_WAL) {
  262.       mprintf("read wal %s\t-> ", procName);
  263.       inPic = 0;    /* GetWal(procFile, 0); */
  264.     }
  265.     else if (procType == TYPE_LUMP) {
  266.       mprintf("read lump %s\t-> ", procName);
  267.       inPic = GetLMP(procFile, arcName);
  268.     }
  269.     else if (procType == TYPE_IMAGE) {
  270.       short int alignX = 1, alignY = 1;
  271.       
  272.       /*
  273.        * special size-rules if we convert to a mipmap
  274.        */
  275.       if ((outType == TYPE_MIPMAP) || (outType == TYPE_WAD2) || (outType == TYPE_BSP)) {
  276.         alignX = alignY = 16;
  277.         if (outName[0] == WARP_MIPMAP)
  278.           alignX = alignY = WARP_X;
  279.         else if (!strncmp(outName, SKY_MIPMAP, 3)) {
  280.           alignX = -(SKY_X);
  281.           alignY = -(SKY_Y);
  282.         }
  283.       }
  284.       mprintf("read image %s\t-> ", procName);
  285.       inPic = GetImage(procFile, arcName, alignX, alignY);
  286.     }
  287.     else if (procType == TYPE_TRIANGLE) {
  288.       mprintf("read tri %s\t-> ", procName);
  289.       inData = 0;
  290.     }
  291.     else if (procType == TYPE_IMAGINE) {
  292.       mprintf("read iob %s\t-> ", procName);
  293.       inData = GetRaw(procFile, procName, 0);
  294.     }
  295.     else if (procType == TYPE_MAP) {
  296.       mprintf("read map %s\t-> ", procName);
  297.       inData = GetRaw(procFile, procName, 0);
  298.     }
  299.     else {
  300.       mprintf("read raw %s\t-> ", procName);
  301.       inData = GetRaw(procFile, procName, 0);
  302.     }
  303.  
  304.     if (inPic || inData) {
  305.       /*
  306.        * convert to ppm 
  307.        */
  308.       if (((outType == TYPE_PPM)
  309.         || (outType == TYPE_JPEG)
  310.         || (outType == TYPE_ILBM)
  311.         || (outType == TYPE_PNG)) && inPic) {
  312.         mprintf("write image %s\n", outName);
  313.         if ((destFile = fopen(outName, WRITE_BINARY))) {
  314.           if (PutImage(destFile, inPic, outType) == FALSE)
  315.         eprintf("failed to convert pic\n");
  316.           fclose(destFile);
  317.         }
  318.         else
  319.           eprintf("cannot open %s\n", outName);
  320.       }
  321.       /*
  322.        * convert to mip 
  323.        */
  324.       else if ((outType == TYPE_MIPMAP) && inPic) {
  325.         mprintf("write mip %s\n", outName);
  326.         if ((destFile = fopen(outName, WRITE_BINARY))) {
  327.           if (PutMipMap(destFile, inPic) == FALSE)
  328.         eprintf("failed to convert to mip\n");
  329.           fclose(destFile);
  330.         }
  331.         else
  332.           eprintf("cannot open %s\n", outName);
  333.       }
  334.       /*
  335.        * convert to lump
  336.        */
  337.       else if ((outType == TYPE_LUMP) && inPic) {
  338.         mprintf("write lump %s\n", outName);
  339.         if ((destFile = fopen(outName, WRITE_BINARY))) {
  340.           if (PutLMP(destFile, inPic) == FALSE)
  341.         eprintf("failed to convert to lump\n");
  342.           fclose(destFile);
  343.         }
  344.         else
  345.           eprintf("cannot open %s\n", outName);
  346.       }
  347.       /*
  348.        * convert to imagine
  349.        */
  350.       else if (((outType == TYPE_IMAGINE) || (outType == TYPE_MAP)) && ((procType == TYPE_MAP) || (procType == TYPE_IMAGINE)) && inData) {
  351.         struct memory bspStatic;
  352.         __memBase = &bspStatic;
  353.         bool retval = FALSE;
  354.         
  355.         mprintf("write to %s\n", outName);
  356.         if ((destFile = fopen(outName, outType == TYPE_MAP ? "w" : WRITE_BINARY))) {
  357.           AllocClusters(bspMem, ALL_MAPS);
  358.           
  359.           if (procType == TYPE_MAP)
  360.             retval = LoadMapFile(bspMem, inData->rawdata);
  361.           else
  362.             retval = LoadTDDDFile(bspMem, inData->rawdata);
  363. printf("loaded\n");
  364.           if (retval == TRUE) {
  365.             if (outType == TYPE_MAP)
  366.               retval = SaveMapFile(bspMem, destFile);
  367.             else
  368.               retval = SaveTDDDFile(bspMem, destFile);
  369. printf("saved\n");
  370.             if (retval != TRUE)
  371.               eprintf("failed to save %s\n", outName);
  372.           }
  373.           else
  374.             eprintf("failed to parse %s\n", procName);
  375.  
  376.           FreeClusters(bspMem, 0);
  377.           fclose(destFile);
  378.         }
  379.         else
  380.           eprintf("cannot open %s\n", outName);
  381.       }
  382.       /*
  383.        * OP_ADD to pak-file 
  384.        */
  385.       else if (outType == TYPE_PACK) {
  386.         mprintf("write to %s to pak %s\n", procName, outName);
  387.         if (AddPAK(inPic, inData, outName, procOper) != TRUE)
  388.           eprintf("failed to add %s to pak %s\n", procName, outName);
  389.       }
  390.       /*
  391.        * OP_ADD to wad2-file 
  392.        */
  393.       else if (outType == TYPE_WAD2) {
  394.         mprintf("write to wad %s\n", outName);
  395.         if (AddWAD2(inPic, inData, outName, procOper, arcType) != TRUE)
  396.           eprintf("failed to add %s to wad %s\n", procName, outName);
  397.       }
  398.       /*
  399.        * OP_ADD to bsp-file 
  400.        */
  401.       else if (outType == TYPE_BSP) {
  402.         mprintf("write to bsp %s\n", outName);
  403.         if (AddBSP(inPic, inData, outName, procOper, arcType) != TRUE)
  404.           eprintf("failed to add %s to bsp %s\n", procName, outName);
  405.       }
  406.       /*
  407.        * OP_ADD to mdl-file 
  408.        */
  409.       else if (outType == TYPE_MODEL) {
  410.       }
  411.       /*
  412.        * OP_ADD to spr-file 
  413.        */
  414.       else if (outType == TYPE_SPRITE) {
  415.       }
  416.       else
  417.         eprintf("dont know how to convert %s to %s\n", procName, outName);
  418.  
  419.       /*
  420.        * tfree resources 
  421.        */
  422.       if (inPic)
  423.         pfree(inPic);
  424.       else if (inData)
  425.         rfree(inData);
  426.     }
  427.     else
  428.       eprintf("failed to read %s\n", procName);
  429.       }
  430.       else if (procType == TYPE_QUAKEC) {
  431.         char srcDir[NAMELEN_PATH], *tmp;
  432.           
  433.         strncpy(srcDir, procName, NAMELEN_PATH - 1);
  434.         /* find parent dir */
  435.         if((tmp = (char *)rindex(srcDir, '/')))
  436.           tmp[1] = '\0';
  437.         else
  438.           srcDir[0] = '\0';
  439.         if(qcc(procFile, srcDir, procOper) != TRUE)
  440.           eprintf("cannot compile full %s\n", procName);
  441.       }
  442.       /*
  443.        * archives to archive 
  444.        */
  445.       else if ((outType == TYPE_PACK) && (procType != TYPE_PACK)) {
  446.     mprintf("add %s %s to pak %s\n", procType, procName, outName);
  447.     if ((inData = GetRaw(procFile, arcName, 0))) {
  448.       if (AddPAK(0, inData, outName, procOper) != TRUE)
  449.         eprintf("failed to add %s to pak %s\n", procName, outName);
  450.       rfree(inData);
  451.     }
  452.     else
  453.       eprintf("failed to read %s\n", procName);
  454.       }
  455.       /*
  456.        * archive to parts 
  457.        */
  458.       else if ((procType == TYPE_PACK) ||
  459.            (procType == TYPE_WAD2) ||
  460.            (procType == TYPE_SPRITE) ||
  461.            (procType == TYPE_MODEL) ||
  462.            (procType == TYPE_CODE) ||
  463.            (procType == TYPE_BSP)) {
  464.     if (script == TRUE) {
  465.       if ((scrpFile = fopen(idxName, WRITE_BINARY))) {
  466.         fprintf(scrpFile, "process %s\n", procName);
  467.         if (dither == TRUE)
  468.           fprintf(scrpFile, "dither %1d with %3d\n", 1, dithervalue);
  469.         else
  470.           fprintf(scrpFile, "dither %1d with %3d\n", 0, dithervalue);
  471.         if (Compression != CMP_NONE)
  472.           fprintf(scrpFile, "compress %1d with %3d\n", 1, Compression);
  473.         else
  474.           fprintf(scrpFile, "compress %1d with %3d\n", 0, Compression);
  475.       }
  476.     }
  477.     if ((!destDir) || (*destDir == '\0')) {
  478.       ReplaceExt(idxName, "dir/");
  479.       destDir = idxName;
  480.     }
  481.  
  482.     /*
  483.      * pak to parts 
  484.      */
  485.     if (procType == TYPE_PACK) {
  486.       mprintf("extract %s from pak %s\n", arcName, procName);
  487.       if (ExtractPAK(procFile, scrpFile, destDir, arcName, outType, procOper, recurse) != TRUE)
  488.         eprintf("failed to extract full pak %s\n", procName);
  489.     }
  490.     /*
  491.      * wad to parts 
  492.      */
  493.     else if (procType == TYPE_WAD2) {
  494.       mprintf("extract %s from wad %s\n", arcName, procName);
  495.       if (ExtractWAD2(procFile, scrpFile, destDir, arcName, outType, procOper, arcType) != TRUE)
  496.         eprintf("failed to extract full wad %s\n", procName);
  497.     }
  498.     /*
  499.      * bsp to parts 
  500.      */
  501.     else if (procType == TYPE_BSP) {
  502.       mprintf("extract %s from bsp %s\n", arcName, procName);
  503.       if (ExtractBSP(procFile, scrpFile, destDir, arcName, outType, procOper, recurse) != TRUE)
  504.         eprintf("failed to extract full bsp %s\n", procName);
  505.     }
  506.     /*
  507.      * mdl to parts 
  508.      */
  509.     else if (procType == TYPE_MODEL) {
  510.     }
  511.     /*
  512.      * spr to parts 
  513.      */
  514.     else if (procType == TYPE_SPRITE) {
  515.     }
  516.     /*
  517.      * code to parts 
  518.      */
  519.     else if (procType == TYPE_CODE) {
  520.           if(unqcc(procFile, destDir, procOper) != TRUE)
  521.             eprintf("cannot decompile full %s to %s\n", procName, destDir);
  522.     }
  523.  
  524.     if (scrpFile) {
  525.       fprintf(scrpFile, "finish %s\n", procName);
  526.       fclose(scrpFile);
  527.       scrpFile = 0;
  528.     }
  529.       }
  530.       else
  531.     eprintf("dont know what to convert %s\n", procName);
  532.     }
  533.     fclose(procFile);
  534.   }
  535.   else
  536.     eprintf("cannot open %s\n", procName);
  537.  
  538.   return TRUE;
  539. }
  540.  
  541. /*
  542.  * one of outName and outType MUST be given
  543.  */
  544. bool processName(char *procName, char *destDir, char *outName, filetype outType, char *arcName, filetype arcType,
  545.          operation procOper, bool script, bool recurse) {
  546.   char *procExt = GetExt(procName);
  547.   char *outExt = GetExt(outName);
  548.   filetype procType = TYPE_UNKNOWN;
  549.   char newName[NAMELEN_PATH + 1];
  550.   
  551.   /*
  552.    * set default proctype
  553.    */
  554.   if(!strcmp(procExt, "pal"))      { procType = TYPE_PALETTE; }
  555.   else if(!strcmp(procExt, "bsp")) { procType = TYPE_BSP; }
  556.   else if(!strcmp(procExt, "cfg")) { procType = TYPE_CONFIG; }
  557.   else if(!strcmp(procExt, "dat")) { procType = TYPE_CODE; }
  558.   else if(!strcmp(procExt, "dem")) { procType = TYPE_DEMO; }
  559.   else if(!strcmp(procExt, "dir")) { procType = TYPE_DIRECTORY; }
  560.   else if(!strcmp(procExt, "frm")) { procType = TYPE_LUMP; }
  561.   else if(!strcmp(procExt, "idx")) { procType = TYPE_INDEX; }
  562.   else if(!strcmp(procExt, "iff")) { procType = TYPE_IMAGE; }
  563.   else if(!strcmp(procExt, "iob")) { procType = TYPE_IMAGINE; }
  564.   else if(!strcmp(procExt, "jpg")) { procType = TYPE_IMAGE; }
  565.   else if(!strcmp(procExt, "lbm")) { procType = TYPE_IMAGE; }
  566.   else if(!strcmp(procExt, "lit")) { procType = TYPE_LIT; }
  567.   else if(!strcmp(procExt, "lmp")) { procType = TYPE_LUMP; }
  568.   else if(!strcmp(procExt, "map")) { procType = TYPE_MAP; }
  569.   else if(!strcmp(procExt, "mdl")) { procType = TYPE_MODEL; }
  570.   else if(!strcmp(procExt, "mip")) { procType = TYPE_MIPMAP; }
  571.   else if(!strcmp(procExt, "pak")) { procType = TYPE_PACK; }
  572.   else if(!strcmp(procExt, "png")) { procType = TYPE_IMAGE; }
  573.   else if(!strcmp(procExt, "ppm")) { procType = TYPE_IMAGE; }
  574.   else if(!strcmp(procExt, "pnm")) { procType = TYPE_IMAGE; }
  575.   else if(!strcmp(procExt, "prt")) { procType = TYPE_PRT; }
  576.   else if(!strcmp(procExt, "rc"))  { procType = TYPE_RESOURCE; }
  577.   else if(!strcmp(procExt, "skn")) { procType = TYPE_LUMP; }
  578.   else if(!strcmp(procExt, "spr")) { procType = TYPE_SPRITE; }
  579.   else if(!strcmp(procExt, "src")) { procType = TYPE_QUAKEC; }
  580.   else if(!strcmp(procExt, "stb")) { procType = TYPE_LUMP; }
  581.   else if(!strcmp(procExt, "tri")) { procType = TYPE_TRIANGLE; }
  582.   else if(!strcmp(procExt, "vis")) { procType = TYPE_VIS; }
  583.   else if(!strcmp(procExt, "wad")) { procType = TYPE_WAD2; }
  584.   else if(!strcmp(procExt, "wal")) { procType = TYPE_WAL; }
  585.   else if(!strcmp(procExt, "wav")) { procType = TYPE_WAVE; }
  586.  
  587.   /*
  588.    * set default outtype
  589.    */
  590.   if(outName) {
  591.     if (outType == TYPE_NONE) {
  592.       if(!strcmp(outExt, "pal"))      { outType = TYPE_PALETTE; }
  593.       else if(!strcmp(outExt, "bsp")) { outType = TYPE_BSP; }
  594.       else if(!strcmp(outExt, "cfg")) { outType = TYPE_CONFIG; }
  595.       else if(!strcmp(outExt, "dat")) { outType = TYPE_CODE; }
  596.       else if(!strcmp(outExt, "dem")) { outType = TYPE_DEMO; }
  597.       else if(!strcmp(outExt, "dir")) { outType = TYPE_DIRECTORY; }
  598.       else if(!strcmp(outExt, "frm")) { outType = TYPE_FRAME; }
  599.       else if(!strcmp(outExt, "idx")) { outType = TYPE_INDEX; }
  600.       else if(!strcmp(outExt, "iff")) { outType = TYPE_ILBM; }
  601.       else if(!strcmp(outExt, "iob")) { outType = TYPE_IMAGINE; }
  602.       else if(!strcmp(outExt, "jpg")) { outType = TYPE_JPEG; }
  603.       else if(!strcmp(outExt, "lbm")) { outType = TYPE_ILBM; }
  604.       else if(!strcmp(outExt, "lit")) { outType = TYPE_LIT; }
  605.       else if(!strcmp(outExt, "lmp")) { outType = TYPE_LUMP; }
  606.       else if(!strcmp(outExt, "map")) { outType = TYPE_MAP; }
  607.       else if(!strcmp(outExt, "mdl")) { outType = TYPE_MODEL; }
  608.       else if(!strcmp(outExt, "mip")) { outType = TYPE_MIPMAP; }
  609.       else if(!strcmp(outExt, "pak")) { outType = TYPE_PACK; }
  610.       else if(!strcmp(outExt, "png")) { outType = TYPE_PNG; }
  611.       else if(!strcmp(outExt, "ppm")) { outType = TYPE_PPM; }
  612.       else if(!strcmp(outExt, "pnm")) { outType = TYPE_PPM; }
  613.       else if(!strcmp(outExt, "prt")) { outType = TYPE_PRT; }
  614.       else if(!strcmp(outExt, "rc"))  { outType = TYPE_RESOURCE; }
  615.       else if(!strcmp(outExt, "skn")) { outType = TYPE_SKIN; }
  616.       else if(!strcmp(outExt, "spr")) { outType = TYPE_SPRITE; }
  617.       else if(!strcmp(outExt, "src")) { outType = TYPE_QUAKEC; }
  618.       else if(!strcmp(outExt, "stb")) { outType = TYPE_STATUSBAR; }
  619.       else if(!strcmp(outExt, "tri")) { outType = TYPE_TRIANGLE; }
  620.       else if(!strcmp(outExt, "vis")) { outType = TYPE_VIS; }
  621.       else if(!strcmp(outExt, "wad")) { outType = TYPE_WAD2; }
  622.       else if(!strcmp(outExt, "wal")) { outType = TYPE_WAL; }
  623.       else if(!strcmp(outExt, "wav")) { outType = TYPE_WAVE; }
  624.     }
  625.     strncpy(newName, outName, NAMELEN_PATH - 1);
  626.   }
  627.   /*
  628.    * set default outname
  629.    */
  630.   else {
  631.     strncpy(newName, destDir, NAMELEN_PATH - 1);
  632.     strncat(newName, procName, NAMELEN_PATH - 1);
  633.     switch (outType) {
  634.       case TYPE_PPM:    ReplaceExt(newName, "ppm"); break;
  635.       case TYPE_JPEG:   ReplaceExt(newName, "jpg"); break;
  636.       case TYPE_ILBM:   ReplaceExt(newName, "iff"); break;
  637.       case TYPE_PNG:    ReplaceExt(newName, "png"); break;
  638.       case TYPE_WAL:    ReplaceExt(newName, "wal"); break;
  639.       case TYPE_MIPMAP: ReplaceExt(newName, "mip"); break;
  640.       case TYPE_LUMP:   ReplaceExt(newName, "lmp"); break;
  641.       default:                        break;
  642.     }
  643.   }
  644.  
  645.   /*
  646.    * set default arcname
  647.    *
  648.    * behaviour:
  649.    *  * / *.* -> *.pak    arcName = * / *.*
  650.    *  * / *.* -> *.wad    arcName =     *
  651.    *  * / *.* -> *.bsp    arcName =     *
  652.    *  *.pak -> * / *.*    arcName = * / *.*
  653.    *  *.wad -> * / *.*    arcName =     *
  654.    *  *.bsp -> * / *.*    arcName =     *
  655.    *  *.bsp -> *.wad    arcName = *.wad
  656.    */
  657.   if(!arcName) {
  658.     if((outType == TYPE_PACK) ||
  659.     ((outType == TYPE_WAD2) && (procType != TYPE_WAD2)) ||
  660.     (outType == TYPE_SPRITE) ||
  661.     (outType == TYPE_MODEL) ||
  662.     (outType == TYPE_BSP)) {
  663.       arcName = smalloc(procName);
  664.       if(arcType == TYPE_NONE)
  665.         arcType = procType;
  666.     }
  667.     else {
  668.       arcName = smalloc(outName);
  669.       if(arcType == TYPE_NONE)
  670.         arcType = outType;
  671.     }
  672.   }
  673.   if(!( ((outType == TYPE_PACK) || (procType == TYPE_PACK)) ||
  674.         (((outType == TYPE_MAP) || (outType == TYPE_IMAGINE) || (outType == TYPE_LIT) || (outType == TYPE_VIS) || (outType == TYPE_WAD2)) && (procType == TYPE_BSP)) ||
  675.         ((outType == TYPE_WAD2) && (procType == TYPE_WAD2)) )) {
  676.     arcName = GetFile(arcName);
  677.     StripExt(arcName);
  678.   }
  679.   
  680.   /*
  681.    * if the dirname doesn't end with / we must add it, hmpf
  682.    */
  683.   if(destDir) {
  684.     int len = strlen(destDir);
  685.   
  686.     if(!((destDir[len - 1] == '/') || (destDir[len - 1] == ':') || (destDir[len - 1] == '\0'))) {
  687.       char *newDir;
  688.       
  689.       if((newDir = (char *)tmalloc(len + 2))) {
  690.         strcpy(newDir, destDir);
  691.         newDir[len] = '/';
  692.         destDir = newDir;
  693. printf("%s\n", destDir);
  694.       }
  695.     }
  696.   }
  697.  
  698.   outName = newName;
  699.   return processType(procName, procType, destDir, outName, outType, arcName, arcType, procOper, script, recurse);
  700. }
  701.